@extends('admin.layout')
@section('content')
<!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                用户管理
                <small>用户列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
                <li><a href="#">用户管理</a></li>
                <li class="active">列表</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">快速查看用户</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                        	@if(session('info'))
                        	<div id="jg" class="callout callout-info">
								<p>{{ session('info') }}</p>
							</div>
                        	@endif
							<form action="{{ url('/admin/user/index') }}">
	                    		<div class="row">
									<div class="col-md-2">
	                        			<div class="form-group">
			                        		<select name="num" class="form-control">
			                        			<option
			                        			@if(!empty($request['num']) && $request['num'] == 10)
			                        				selected="selected"
			                        			@endif
			                        			>10</option>
			                        			<option
			                        			@if(!empty($request['num']) && $request['num'] == 25)
			                        				selected="selected"
			                        			@endif
			                        			>25</option>
			                        			<option
			                        			@if(!empty($request['num']) && $request['num'] == 50)
			                        				selected="selected"
			                        			@endif
			                        			>50</option>
			                        			<option
			                        			@if(!empty($request['num']) && $request['num'] == 100)
			                        				selected="selected"
			                        			@endif
			                        			>100</option>
			                        		</select>
			                        	</div>
	                        		</div>
	                        		<div class="col-md-4 col-md-offset-6">
	                        			<div class="input-group">
											<input value="{{ $request['keywords'] or '' }}" name="keywords" type="text" class="form-control">
											<span class="input-group-btn">
												<button class="btn btn-info btn-flat">搜索</button>
											</span>
										</div>
	                        		</div>
	                        	</div>
                        	</form>
                            <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>邮箱</th>
                                    <th>头像</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($data as $v)
                                <tr>
                                    <td class="ids">{{ $v -> id }}</td>
                                    <td class="user_name">{{ $v -> user_name }}</td>
                                    <td>{{ $v -> email }}</td>
                                    <td><img width="50" src="/uploads/yhtx/{{ $v -> photo }}"></td>
                                    <td class="status">
                                    @if($v -> status == 0)
                                    	<button class="btn btn-default btn-sm">禁用</button>
                                    	
                                    @elseif($v -> status == 1)
                                    	<button class="btn btn-default btn-sm">启用</button>
                                    
                                    @endif
                                    </td>
                                    <td>
	                                    <button class="btn btn-default btn-sm"><a href="{{ url('/admin/user/edit/') }}/{{ $v -> id }}">编辑</a></button> 
	                                    <button class="btn btn-default btn-sm"><a href="{{ url('/admin/user/delete/') }}/{{ $v -> id }}">删除</a></button>
	                                </td>
                                </tr>
                                @endforeach
                                </tbody>
                            </table>
                            {!! $data -> appends($request) -> render() !!}
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->

    <script type="text/javascript">
    	window.onload = function()
		{
			//隐藏警告消息
			setInterval(function()
			{
				$('#jg').fadeOut(1000);
			}, 3000);

			$.ajaxSetup({
				headers:{
					'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
				}
			});
			$('.status').on('click', function()
			{
				//获取当前对象
				var t = $(this);

				//获取id
				var id = $(this).parent().find('.ids').html();
				$.ajax({
					type:"POST",
					url:"{{ url('/admin/user/ajaxStatus') }}",
					data:{id:id},
					success:function(data)
					{
						if(data == 2)
						{
							alert('修改失败');
						}else if(data == 0)
						{
							t.html('<button class="btn btn-default btn-sm">禁用</button>');
						}else if(data == 1)
						{
							t.html('<button class="btn btn-default btn-sm">启用</button>');
						}
					},
					error:function()
					{
						alert('异常');
					}
				});
			});

			//双击修改用户名
			$(".user_name").on('dblclick', dblClick);

			function dblClick()
			{
				//获取id
				var id = $(this).parent().find('.ids').html();

				//准备inp
				var inp = $("<input type='text'>");
				//获取原来的值
				var oldName = $(this).html();
				//添加原来的值到inp
				inp.val(oldName);
				//修改td内容
				$(this).html(inp);

				inp.select();

				var t = $(this);

				$(inp).on('blur', function()
				{
					//获取新的inp内容
					var newName = inp.val();
					$.ajax({
						type:"POST",
						url:"{{ url('/admin/user/ajaxName') }}",
						data:{id:id,user_name:newName},
						success:function(data)
						{
							if(data == 4)
							{
								alert('用户名已经存在');
								t.html(oldName);
							}else if(data == 0)
							{
								alert('修改失败');
								t.html(oldName);
							}else if(data == 1)
							{
								t.html(newName);
							}
						},
						error:function()
						{
							alert('异常');
						}
					});

					//注册事件
					t.on('dblclick', dblClick);
				});

				t.unbind('dblclick');
			}
		}
	</script>

@endsection()